'use strict'
var mongoose = require('mongoose')
var User = mongoose.model('User')

exports.signature = function *(next) {
	var body = this.request.body
	var type = body.type
	var timestamp = body.timestamp
	var folder
	var tags

	if (type === 'avatar') {
		folder = 'avatar'
		tags = 'app,avatar'
	} else if (type === 'video') {
		folder = 'video'
		tags = 'app,video'
	} else if (type === 'audio') {
		folder = 'audio'
		tags = 'app,audio'
	}

	secret

	this.body = {
		success: true
	}
}

exports.hasBody = function *(next) {
	var body = this.request.body || {}
	if (Object.keys(body).length === 0) {
		this.body = {
			success: false,
			err: 'body体为空'
		}
		return next
	}
	yield next

}


exports.hasToken = function *(next) {
	var accessToken = this.request.query.accessToken
	if (!accessToken) {
		var accessToken = this.request.body.accessToken
	}
	if (!accessToken) {
		this.body = {
			success: false,
			err: '密钥不存在'
		}
		return next
	}

	var user = yield User.findOne({
		accessToken: accessToken
	}).exec()

	if (!user) {
		this.body = {
			success: false,
			err: '用户为登录'
		}
		return next
	}

	this.session = this.session || {}

	this.session.user = user

	yield next
}